﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;

/// <summary>
/// SendCoupon 的摘要说明
/// </summary>
public class SendCoupon
{
    public SendCoupon()
    {
       
    }


    /// <summary>
    /// 给会员发代金券
    /// </summary>
    /// <param name="strMemID">会员编号字符创</param>
    /// <param name="intNumber">发送数量</param>
    /// <param name="intCouponID">代金券编号</param>
    /// <param name="UserModel">SysUser类型，为了要商家编号和操作员编号</param>
    /// <returns></returns>
    public int SendCouponToMem(String strMemID,int intNumber,int intCouponID,Chain.Model.SysUser UserModel)
    {
        int flag = 0;
        try
        {
            int intUserShopID = UserModel.UserShopID;
            int intUserID = UserModel.UserID;

            Chain.Model.SysShop modelShop = new Chain.BLL.SysShop().GetModel(intUserShopID);
            Chain.BLL.Coupon bllCoupon = new Chain.BLL.Coupon();
            Chain.Model.Coupon modelCoupon = new Chain.Model.Coupon();
            modelCoupon = bllCoupon.GetModel(intCouponID);
            if (int.Parse(SMSInfo.GetBalance(false)) < intNumber)
            {
                flag = -1;
            }
            else if (PubFunction.IsCanSendSms(UserModel.UserShopID, strMemID.Split(new char[] { ',' }).Length))
            {
                if (modelCoupon.CouponPredictNu < intNumber)
                {
                    flag = -2;
                }
                else
                {
                    StringBuilder strCouponContent = new StringBuilder(modelCoupon.CouponContent);
                    strCouponContent.Replace("{店铺}", modelShop.ShopName);
                    strCouponContent.Replace("{优惠券}", modelCoupon.CouponTitle);
                    string strTime = "";
                    if (modelCoupon.CouponEnd.HasValue)
                    {
                        strTime = DateTime.Parse(modelCoupon.CouponEnd.ToString()).ToShortDateString();
                    }
                    else
                    {
                        strTime = "永久";
                    }
                    strCouponContent.Replace("{有效期}", strTime);
                    string strCouponMoney = Math.Round(modelCoupon.CouponMinMoney, 2).ToString();
                    strCouponContent.Replace("{最低消费}", strCouponMoney);
                    strCouponContent.Replace("{店面电话}", modelShop.ShopTelephone);
                    if (PubFunction.curParameter.bolIsSmsShopName)
                    {
                        if (PubFunction.curParameter.strSmsShopName != "")
                        {
                            strCouponContent.Append("【" + PubFunction.curParameter.strSmsShopName + "】");
                        }
                    }
                    else if (modelShop.ShopSmsName != "")
                    {
                        strCouponContent.Append("【" + modelShop.ShopSmsName + "】");
                    }
                    Chain.BLL.CouponList bllCouponList = new Chain.BLL.CouponList();
                    DataTable dtCouponList = bllCouponList.GetList(intNumber, " CouPonID=" + intCouponID + " and CouPonYF='False' ", " CID ").Tables[0];
                    List<Chain.Model.CouponList> listCoupon = bllCouponList.DataTableToList(dtCouponList);
                    listCoupon.Sort(delegate (Chain.Model.CouponList p1, Chain.Model.CouponList p2)
                    {
                        return Comparer<int>.Default.Compare(p1.CID, p2.CID);
                    });
                    ArrayList arraySql = new ArrayList();
                    string[] strMemArray = Regex.Split(strMemID, ",", RegexOptions.IgnoreCase);
                    for (int i = 0; i < intNumber; i++)
                    {
                        if (strMemArray[i] != "")
                        {
                            StringBuilder str = new StringBuilder(strCouponContent.ToString());
                            str.Replace("{券号}", listCoupon[i].CouPon);
                            Chain.Model.Mem modelMem = new Chain.BLL.Mem().GetModel(int.Parse(strMemArray[i]));
                            if (SMSInfo.Send_GXSMS(false, modelMem.MemMobile, str.ToString(), ""))
                            {
                                Chain.Model.SmsLog modelSms = new Chain.Model.SmsLog();
                                modelSms.SmsMemID = modelMem.MemID;
                                modelSms.SmsMobile = modelMem.MemMobile;
                                modelSms.SmsContent = str.ToString();
                                modelSms.SmsTime = DateTime.Now;
                                modelSms.SmsShopID = intUserShopID;
                                modelSms.SmsUserID = intUserID;
                                modelSms.SmsAmount = PubFunction.GetSmsAmount(str.ToString());
                                modelSms.SmsAllAmount = modelSms.SmsAmount;
                                new Chain.BLL.SmsLog().Add(modelSms);
                                string strSql = ("update CouponList set CouPonYF='True',CouPonMID=" + strMemArray[i] + ",ConPonSendTime='" + DateTime.Now.ToString()) + "' where Coupon='" + listCoupon[i].CouPon + "';";
                                arraySql.Add(strSql);
                            }
                        }
                    }
                    if (bllCouponList.DataUpdateTran(arraySql))
                    {
                        modelCoupon.CouponYF += intNumber;
                        bllCoupon.Update(modelCoupon);
                        flag = 1;
                        PubFunction.SaveSysLog(intUserID, 4, "优惠券", string.Concat(new object[] { "批量发放优惠券，发送", intNumber, "人，优惠去名称[", modelCoupon.CouponTitle, "]" }), intUserShopID, DateTime.Now, PubFunction.ipAdress);
                    }
                }
                if (PubFunction.curParameter.bolShopSmsManage)
                {
                    PubFunction.SetShopSms(intUserID, UserModel.UserShopID, strMemID.Split(new char[] { ',' }).Length, 2);
                }
            }
            else
            {
                flag = -5;
            }
        }
        catch (Exception e)
        {           
            flag = 0;
        }
        return flag;
    }
}